package org.apache.struts.ehr.patient_details.action;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

import org.apache.struts.ehr.medical_records.model.MergePatientLog;
import org.apache.struts.ehr.patient_details.model.HL7Message;


import ca.uhn.hl7v2.model.v251.message.ACK;
import ca.uhn.hl7v2.model.v251.message.ADR_A19;
import ca.uhn.hl7v2.model.v251.message.ADT_A61;
import ca.uhn.hl7v2.model.v251.segment.PID;
import ca.uhn.hl7v2.model.v251.segment.PV1;

import com.earogya.communicate.Communicator;
import com.earogya.logic.medical_records.CreateADT34;
import com.earogya.logic.patient_details.ADR_A19_Handler;
import com.earogya.logic.patient_details.CreateADTA34;
import com.earogya.logic.patient_details.CreateQRYA19;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class MergePatientAction extends ActionSupport{

	private HL7Message message;
	private int patientId1;
	private int patientId2;
	private int currentPatientId;
	private int newPatientId;


	
	public int getPatientId1() {
		return patientId1;
	}

	public void setPatientId1(int patientId1) {
		this.patientId1 = patientId1;
	}

	public int getPatientId2() {
		return patientId2;
	}

	public void setPatientId2(int patientId2) {
		this.patientId2 = patientId2;
	}

	public String execute()throws Exception{
		boolean isSuccess=false;
		Communicator c=new Communicator();
		Map<String, Object> session=ActionContext.getContext().getSession();
		if(patientId1>patientId2){
			newPatientId=patientId1;
			currentPatientId=patientId2;
		}
		else if(patientId1<patientId2){
			newPatientId=patientId2;
			currentPatientId=patientId1;
		}
		else{
			System.out.print("Patient Ids are same. Can't merge");
		}
		if(patientId1!=patientId2){
		CreateADT34 createMessage=new com.earogya.logic.medical_records.CreateADT34(new MergePatientLog(currentPatientId,newPatientId,new GregorianCalendar().getInstance().getTime(),"Nurse"));
		String msg=createMessage.createMessage((String)session.get("facility"));
		message=new HL7Message();
		if(!msg.equals(null)){
			
			String reply=c.sendMessage(msg);
			message.setMessage(msg+"-------------"+reply);
			System.out.print(reply);
		isSuccess=true;
		if(isSuccess==true){
			CreateADTA34 createMessage2=new com.earogya.logic.patient_details.CreateADTA34(newPatientId,currentPatientId);
			String msg2=createMessage2.createMessage((String)session.get("facility"));
			message=new HL7Message();
			if(!msg.equals(null)){
				String reply2=c.sendMessage(msg2);
				message.setMessage(msg2+"-------------"+reply2);
				System.out.print(reply2);
				}
			else{
				message.setMessage("empty");
			}
			
		}
		}
				else{
			message.setMessage("empty");
		}
		
		
		}
		return SUCCESS;
	}
	
	public void validate(){
		
	}
}
